package com.luoshi.ucenterservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.luoshi.common.exception.ucenter.MobileExistException;
import com.luoshi.common.exception.ucenter.MobileIsDisabledException;
import com.luoshi.common.exception.ucenter.MobileOrPasswordException;
import com.luoshi.common.exception.ucenter.PasswordException;
import com.luoshi.common.result.ResultCodeEnum;
import com.luoshi.common.util.JwtUtils;
import com.luoshi.common.util.MD5;
import com.luoshi.entity.base.ucenter.UcenterMember;
import com.luoshi.entity.base.ucenter.vo.LoginVo;
import com.luoshi.ucenterservice.mapper.UcenterMemberMapper;
import com.luoshi.ucenterservice.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName UcenterMemberServiceImpl
 * @Description:会员服务实现
 * @Author luochangqin
 * @Date 2021/5/23
 * @Version V1.0
 **/
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    /**
     * 手机登录方法
     * @param login
     * @return
     */
    @Override
    public Map<String,Object> login(LoginVo login) throws Exception {
        // 获取手机号码
        String mobile = login.getMobile();
        //获取密码
        String password = login.getPassword();

        // 手机和密码非空判断
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw new MobileOrPasswordException(ResultCodeEnum.NAMEORPASS_IS_NULL);
        }

        // 判断手机号码是否正确
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        //wrapper.eq("password", MD5.encrypt(password));
        UcenterMember ucenterMember = baseMapper.selectOne(wrapper);
        // 判断查询对象是否为空
        if (ucenterMember == null) {
            throw new MobileOrPasswordException(ResultCodeEnum.NAME_ERROR);
        }

        // 判断用户密码是否正确
        if(!MD5.encrypt(password).equals(ucenterMember.getPassword())){
            throw new PasswordException(ResultCodeEnum.PASSWORD_ERROR);
        }
        // 判断用户是否禁用
        if (!ucenterMember.getIsDisabled()) {
            throw new MobileIsDisabledException(ResultCodeEnum.MOBLE_ISDISBLE_ERROR);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        String token = JwtUtils.getJwtToken(ucenterMember.getId(), ucenterMember.getMobile());
        map.put("token", token);
        return map;
    }
    // 手机注册
    @Override
    public Map<String,Object> register(LoginVo loginVo) throws Exception{
        // 获取注册的昵称
        String  mobile = loginVo.getMobile();
        //获取注册的密码
        String password = loginVo.getPassword();

        // 手机和密码非空判断
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw new MobileOrPasswordException(ResultCodeEnum.NAMEORPASS_IS_NULL);
        }
        // 判断昵称系统中是否存在此用户
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if(count >0){
            throw  new MobileExistException(ResultCodeEnum.MOBLE_EXISTL_ERROP);
        }

        // 添加到数据库中
        UcenterMember  member = new UcenterMember();
        member.setMobile(mobile);
        member.setPassword(MD5.encrypt(password)); //密码需要加密
        member.setAvatar("https://luoqing01.oss-cn-chengdu.aliyuncs.com/2021/03/29/3325360145d9487790c77078bea7c0b4file.png");
        member.setIsDisabled(true); // 用户可用
        int numb = baseMapper.insert(member);
        Map<String,Object> map = new HashMap<>();
        String jwtToken = JwtUtils.getJwtToken(member.getId(), member.getMobile());
        map.put("token",jwtToken);
        return map;
    }
}
